<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <!-- 插入到left插槽位置 -->
        <template #left>
          <span>本月: 社保在缴 公积金在缴</span>
        </template>
        <!-- 插入到right插槽位置 -->
        <template #right>
          <!--  v-if="$store.state.user.userInfo.data.roles.points.includes('import_excel')" -->
          <el-button
            v-allow="'import_excel'"
            type="warning"
            size="small"
            @click="$router.push('/import')"
          >导入excel</el-button>
          <!-- v-if="$store.state.user.userInfo.data.roles.points.includes('exprot_excel')" -->
          <el-button
            v-allow="'exprot_excel'"
            type="danger"
            size="small"
            @click="hExport"
          >导出excel</el-button>
          <el-button
            type="primary"
            size="small"
            @click="showDialog = true"
          >新增员工</el-button>
        </template>
      </page-tools>
      <!-- <Student/> -->
      <el-card style="margin-top: 10px">
        <el-table border :data="list" :default-sort="{ prop: 'workNumber' }">
          <el-table-column type="index" label="序号" />
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="staffPhoto" label="头像">
            <template slot-scope="scope">
              <ImageHolder :src="scope.row.staffPhoto" />
            </template>
          </el-table-column>
          <el-table-column prop="workNumber" label="工号" />
          <el-table-column prop="formOfEmployment" label="聘用形式">
            <template slot-scope="scope">
              <!-- {{scope.row.formOfEmployment===1?'正式':'非正式'}} -->
              {{ format(scope.row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" sortable label="入职时间" />
          <!-- <el-table-column label="账户状态" /> -->
          <el-table-column label="操作" width="280">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="small"
                @click="$router.push('/employees/detail?id=' + scope.row.id)"
              >查看</el-button>
              <el-button
                type="text"
                size="small"
                @click="hAssignRole(scope.row.id)"
              >分配角色</el-button>
              <el-button
                type="text"
                size="small"
                @click="hDel(scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <el-pagination
            :total="total"
            :current-page="page"
            :page-size="size"
            :page-sizes="[3, 10, 20]"
            layout="sizes,prev, pager, next"
            @current-change="hCurrentChange"
            @size-change="hSizeChange"
          />
        </el-row>
      </el-card>
    </div>
    <el-dialog
      title="添加"
      :close-on-click-modal="true"
      :close-on-press-escape="true"
      :visible.sync="showDialog"
    >
      <emp-dialog v-if="showDialog" @update="hUpdateEmpolyee" />
    </el-dialog>
    <!-- 角色管理 -->
    <el-dialog :visible.sync="showDialogRole">
      <AssignRole v-if="showDialogRole" :cur-id="curId" @close="change" />
    </el-dialog>
  </div>
</template>

<script>
// import AssignRole from './assignRole.vue'
import AssignRole from '@/views/employees/assignRole.vue'
// const type_map ={1:'正式',2:'非正式'}
import { typemap } from '@/constant/index'
import { getEmployeeList, delEmployee } from '@/api/employees'
import empDialog from './empDialog.vue'
export default {
  components: { empDialog, AssignRole },
  data() {
    return {
      list: [],
      // 分页
      total: 0,
      page: 1,
      size: 3,
      // 添加
      showDialog: false,
      // 角色管理
      showDialogRole: false,
      curId: ''
    }
  },
  created() {
    // 1. 调用axios 获取员工数据
    this.loadEmployeeList()
  },
  methods: {
    change() {
      this.showDialogRole = false
    },
    hAssignRole(id) {
      console.log(1)
      this.showDialogRole = true
      this.curId = id
    },
    // 导出文件
    formatData(rows) {
      const mapobj = {
        id: '编号',
        password: '密码',
        mobile: '手机号',
        username: '姓名',
        timeOfEntry: '入职日期',
        formOfEmployment: '聘用形式',
        correctionTime: '转正日期',
        workNumber: '工号',
        departmentName: '部门',
        staffPhoto: '头像地址'
      }
      console.log(rows)
      // 表头
      const enHeader = Object.keys(rows[0]) // 找到英文的表头
      const header = enHeader.map((en) => mapobj[en]) // 转换成中文的表头
      // 内容
      const data = rows.map((obj) => {
        obj.formOfEmployment = typemap[obj.formOfEmployment]
        return Object.values(obj)
      })
      return { header, data }
    },
    async hExport() {
      // 1  发送axios 获取数据
      const res = await getEmployeeList({ page: this.page, size: this.size })
      console.log(res)
      // 2 对数据进行格式转换
      const { header, data } = this.formatData(res.data.rows)
      // 3 导出
      console.log('员工信息', header, data)
      import('@/vendor/Export2Excel').then((excel) => {
        // excel表示导入的模块对象
        console.log(excel)
        excel.export_json_to_excel({
          header: header, // 表头 必填
          data: data, // 具体数据 必填
          filename: 'excel-list', // 文件名称
          autoWidth: true, // 宽度是否自适应
          bookType: 'xlsx' // 生成的文件类型
        })
      })
    },
    hUpdateEmpolyee() {
      this.showDialog = false
      /*       // 添加成功，进入最后一页看数据
      this.total++
      // 最后一页
      this.pageParams.page = Math.ceil(this.total / this.pageParams.size) */
      this.loadEmployeeList()
    },
    async doDel(id) {
      const res = await delEmployee(id)
      console.log('删除', res)
      if (this.list.length === 1 && this.page > 1) {
        this.page--
      }
      this.$message.success('删除成功')
      this.loadEmployeeList()
    },
    hDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.doDel(id)
        })
        .catch(() => {})
    },
    // 分页
    hCurrentChange(curPage) {
      // 1. 更新页码
      this.page = curPage
      // 2. 重发请求
      this.loadEmployeeList()
    },
    hSizeChange(val) {
      this.size = val
      this.page = 1
      this.loadEmployeeList()
    },
    // 表格数据格式化typemap的值等于数据传过来的值
    format(type) {
      return typemap[type]
    },
    async loadEmployeeList() {
      const res = await getEmployeeList({ page: this.page, size: this.size })
      // console.log('res',res)
      // 2.数据保存到data  3.渲染到el-table el-table：data el-table -column：prop
      this.list = res.data.rows
      this.total = res.data.total
    }
  }
}
</script>

<style></style>
